package academic.ui.client.presenter;


import java.util.Map;


import ui.client.GenericClickable;

import academic.ui.client.place.Tokens;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class AccountPresenter extends Presenter<AccountPresenter.MyView,AccountPresenter.MyProxy>
{
	
	private final PlaceManager placeManager;

	private MyDealsPresenter dealsPresenter;
	private MyAccountPresenter accountPresenter;
	private MyGroupsPresenter groupsPresenter;
	
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> tabSlot = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public AccountPresenter(EventBus eventBus, MyView view, MyProxy proxy,
			MyDealsPresenter dealsPresenter,MyAccountPresenter accountPresenter,MyGroupsPresenter groupsPresenter,PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.placeManager=placeManager;
		this.accountPresenter = accountPresenter;
		this.dealsPresenter = dealsPresenter;
		this.groupsPresenter = groupsPresenter;
	}

	@ProxyStandard	
	@NameToken(Tokens.myAccount)
	public interface MyProxy extends ProxyPlace<AccountPresenter>{

	}
	
	public static String MYDEALSTAB ="myDeals";
	public static String MYGROUPSTAB ="myGroups";
	public static String MYACCOUNTTAB ="myAccount";

	
	public interface MyView extends View {

		void updateTab(String parameter);

		Map<String, GenericClickable> getTabHandlers();

	
	}
	
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("AccountPresenter onReveal");
		
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("AccountPresenter onBind");
		
		
		
		for(final String key : getView().getTabHandlers().keySet())
		{
			registerHandler(getView().getTabHandlers().get(key).addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					placeManager.revealPlace(new PlaceRequest(Tokens.myAccount).with("tab", key));
				}
				
			}));
		}
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
		
	}
	
	@Override
	public void prepareFromRequest(PlaceRequest request) {
		if(request.getParameter("tab", null) !=null)
			getView().updateTab(request.getParameter("tab", null));
		
		if(MYACCOUNTTAB.equals(request.getParameter("tab", null)))
		{
			addToSlot(tabSlot,accountPresenter);
		}
		else if(MYDEALSTAB.equals(request.getParameter("tab", null)))
		{
			addToSlot(tabSlot,dealsPresenter);
			
		}
		else if(MYGROUPSTAB.equals(request.getParameter("tab",null)))
		{
			addToSlot(tabSlot,groupsPresenter);
		}
	}
    
	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, AppPresenter.slot, this);
		Log.debug("AccountPresenter revealInParent");
	}
	
	@Override
	public void onReset() {
		super.onReset();
		Log.debug("AccountPresenter onReset");
	}
	
	

}
